//
//  PTCoreManager.m
//  PlanTech
//
//  Created by ruifeng song on 2018/7/16.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "PTCoreManager.h"
#import "PTMObject.h"
#import "PTCoreObject.h"

static id handleSerializeJsonObject(Class resultClass, id jsonObject)
{
    if([resultClass isSubclassOfClass:[PTMObject class]]) {
        if([jsonObject isKindOfClass:[NSDictionary class]]) {
            return [[resultClass class] serializeWithJsonObject:jsonObject];
        }
        
        if([jsonObject isKindOfClass:NSArray.class]) {
            return [[resultClass class] serializeWithJsonObjects:jsonObject];
        }
    }
    return jsonObject;
}


@implementation PTCoreManager
{
    
}

-(instancetype)init
{
    if (self = [super init]) {
        
    }
    
    return self;
}

+ (instancetype)shareInstance {
    static PTCoreManager * _instance = nil;
    static dispatch_once_t _onceToken;
    
    dispatch_once(&_onceToken, ^{
        _instance = [[PTCoreManager alloc] init];
    });
    
    return _instance;
}

-(void)executeRESTfulTaskWithURI:(NSString *)uri
                                                      Method:(nonnull const NSString *)method
                                                      Header:(nullable NSDictionary *)header
                                                  Parameters:(nullable NSDictionary *)parameters
                                                 ResultClass:(nullable Class)resultClass
                                                  Completion:(nullable PTRESTfulCompletionHandler)completion
{
    
    NSString * entireUri = [NSString stringWithFormat:@"%@%@",baseUrl,uri];
    
    if ([method isEqualToString:@"POST"]) {
        
        [CXAFNHttpRequestUtils httpPostRequest:entireUri withParamters:parameters success:^(id responseData) {
        
            __block PTMJsonResult * result = [PTMJsonResult serializeWithJsonObject:responseData];
            
            __block NSError * retError = [PTMJsonResult checkWithResult:result];
            
            if (retError) {
                
                completion(nil,retError);
                return;
            }
            
            __block id<IPTMObject> retObject = nil;
            
            if ([result.content isKindOfClass:[NSDictionary class]]) {
                
                retObject =  handleSerializeJsonObject(resultClass, result.content);
            }
            else
            {
                 retObject =  handleSerializeJsonObject(resultClass, result);
            }

            dispatch_async(dispatch_get_main_queue(), ^{
                
                if (completion) {
                    
                    completion(retObject,nil);
                }
            });
            
        } failure:^(NSError *error, NSInteger errCode) {
            
            __block NSError *retError = [PTMJsonResult checkWithHTTPError:error];
            if (retError) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    completion(nil,error);
                    
                });
                
            }
        }];
    }
    else if([method isEqualToString:@"GET"])
    {
        if (parameters == nil) {
            [CXAFNHttpRequestUtils httpGetRequest:entireUri success:^(id responseData) {
                
                completion(responseData,nil);
                
            } failure:^(NSError *error, NSInteger errCode) {
                completion(nil,error);
            }];
        }
        else
        {
            [CXAFNHttpRequestUtils httpGetRequest:entireUri withParamters:parameters success:^(id responseData) {
                
                NSLog(@"responseData === %@",responseData);
                
                __block PTMJsonResult * result = [PTMJsonResult serializeWithJsonObject:responseData];
                
                __block NSError * retError = [PTMJsonResult checkWithResult:result];
                
                if (retError) {
                    
                    completion(nil,retError);
                    return;
                }
                
                __block id<IPTMObject> retObject =  nil;
                
                if ([result.content isKindOfClass:[NSArray class]] ||[result.content isKindOfClass:[NSDictionary class]]) {
                    
                    retObject =  handleSerializeJsonObject(resultClass, result.content);
                }
                else
                {
                    retObject =  handleSerializeJsonObject(resultClass, result);
                }
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    if (completion) {
                        
                         completion(retObject,nil);
                    }
                });
                
               
                
            } failure:^(NSError *error, NSInteger errCode) {
                
                __block NSError *retError = [PTMJsonResult checkWithHTTPError:error];
                if (retError) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                        completion(nil,error);
                      
                    });
                  
                }
            }];
        }
    }
    else
    {
        
    }
}
@end
